package com.huahua.api.entity.framework;

import lombok.Data;
import lombok.experimental.Accessors;

import java.util.Map;

/**
 * @author Huahua-Ai
 * @date 2025-07-24 16:52:00
 * @description MQTT响应消息格式
 */
@Data
@Accessors(chain = true)
public class MqttResponse {

    /**
     * 命令类型
     */
    private String command;

    /**
     * 请求ID
     */
    private String requestId;

    /**
     * 响应内容
     */
    private ResponseContent response;

    @Data
    @Accessors(chain = true)
    public static class ResponseContent {
        /**
         * 状态：success 或 error
         */
        private String status;

        /**
         * 成功时的数据
         */
        private Map<String, Object> data;

        /**
         * 错误时的错误信息
         */
        private String error;
    }

    /**
     * 创建成功响应
     */
    public static MqttResponse success(String command, String requestId, Map<String, Object> data) {
        ResponseContent response = new ResponseContent()
                .setStatus("success")
                .setData(data);

        return new MqttResponse()
                .setCommand(command)
                .setRequestId(requestId)
                .setResponse(response);
    }

    /**
     * 创建错误响应
     */
    public static MqttResponse error(String command, String requestId, String error) {
        ResponseContent response = new ResponseContent()
                .setStatus("error")
                .setError(error);

        return new MqttResponse()
                .setCommand(command)
                .setRequestId(requestId)
                .setResponse(response);
    }

    /**
     * 判断是否成功
     */
    public boolean isSuccess() {
        return response != null && "success".equals(response.getStatus());
    }
}
